<?php

/**
 * SubtotalService.php
 *
 * @copyright  2022 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-05-12 14:24:05
 * @modified   2023-05-12 14:24:05
 */

namespace Plugin\MultiSeller\Services\TotalServices;

use Beike\Seller\Repositories\SellerRepo;
use Beike\Shop\Services\CheckoutService;

class SubtotalService
{
    /**
     * @param CheckoutService $checkout
     * @return array
     */
    public static function getTotal(CheckoutService $checkout)
    {
        $totalService = $checkout->totalService;

        foreach ($checkout->totalService->getSellerIds() as $sellerId) {
            $amount = $totalService->getSubTotal($sellerId);
            if ($sellerId == 0) {
                $sellerName = trans('MultiSeller::common.self');
            } else {
                $sellerName = SellerRepo::mapIdToStoreName()[$sellerId] ?? 'undefined';
            }
            $totalData = [
                'seller_id'     => $sellerId,
                'code'          => 'sub_total',
                'title'         => trans('shop/carts.product_total') . '-' . $sellerName,
                'amount'        => $amount,
                'amount_format' => currency_format($amount),
            ];

            $totalService->amount += $totalData['amount'];
            $totalService->totals[] = $totalData;
        }

        return $totalData;
    }
}
